package com.example.sistemacompras.utils;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;

public class Utils {

	private Activity mActivity;

	private int mHeight;

	private int mWidth;

	public Utils(Activity activity) {
		this.mActivity = activity;

		Display display = activity.getWindowManager().getDefaultDisplay();
		DisplayMetrics metrics = new DisplayMetrics();
		display.getMetrics(metrics);

		mHeight = metrics.heightPixels;
		mWidth = metrics.widthPixels;

	}

	public float getImageScaleX(Drawable drawable) {
		float width = ((float) drawable.getIntrinsicWidth());
		return (width / mWidth);
	}

	public float getImageScaleY(Drawable drawable) {
		float height = ((float) drawable.getIntrinsicHeight());
		return (height / mHeight);
	}

	public int getStatusBarHeight() {
		int result = 0;
		int resourceId = mActivity.getResources().getIdentifier(
				"status_bar_height", "dimen", "android");

		if (resourceId > 0) {
			result = mActivity.getResources().getDimensionPixelSize(resourceId);
		}

		return result;
	}

	public int getWidthInPixels() {
		return mWidth;
	}

	public int getHeightInPixels() {
		return mHeight;
	}

	public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
		try {
			int orientation = getExifOrientation(src);

			if (orientation == 1) {
				return bitmap;
			}

			Matrix matrix = new Matrix();
			switch (orientation) {
			case 2:
				matrix.setScale(-1, 1);
				break;
			case 3:
				matrix.setRotate(180);
				break;
			case 4:
				matrix.setRotate(180);
				matrix.postScale(-1, 1);
				break;
			case 5:
				matrix.setRotate(90);
				matrix.postScale(-1, 1);
				break;
			case 6:
				matrix.setRotate(90);
				break;
			case 7:
				matrix.setRotate(-90);
				matrix.postScale(-1, 1);
				break;
			case 8:
				matrix.setRotate(-90);
				break;
			default:
				return bitmap;
			}

			try {
				Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0,
						bitmap.getWidth(), bitmap.getHeight(), matrix, true);
				bitmap.recycle();
				return oriented;
			} catch (OutOfMemoryError e) {
				e.printStackTrace();
				return bitmap;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	private static int getExifOrientation(String src) throws IOException {
		int orientation = 1;

		try {
			if (Build.VERSION.SDK_INT >= 5) {
				Class<?> exifClass = Class
						.forName("android.media.ExifInterface");
				Constructor<?> exifConstructor = exifClass
						.getConstructor(new Class[] { String.class });
				Object exifInstance = exifConstructor
						.newInstance(new Object[] { src });
				Method getAttributeInt = exifClass.getMethod("getAttributeInt",
						new Class[] { String.class, int.class });
				Field tagOrientationField = exifClass
						.getField("TAG_ORIENTATION");
				String tagOrientation = (String) tagOrientationField.get(null);
				orientation = (Integer) getAttributeInt.invoke(exifInstance,
						new Object[] { tagOrientation, 1 });
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}

		return orientation;
	}
}
